package wangjg.android.wifiadmin;

import java.util.List;

import android.app.AlertDialog;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.wifi.WifiConfiguration;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

public class WifiAdminActivity extends BasicActivity {
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.wifi_admin);
        this.initUI();
    	this.readWifiSetup();
    }

    private void initUI(){
	    final Spinner spinWifiList = (Spinner)findViewById(R.id.spin_wifiList);
	    spinWifiList.setAdapter(getWifiAA());
	    spinWifiList.setOnItemSelectedListener(new OnItemSelectedListener(){
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				TextView tv = (TextView)view;
				if(tv.getText().length()==0){
					return;
				}
				EditText inputSsid = (EditText)findViewById(R.id.text_ssid);
				inputSsid.setText(tv.getText());
				spinWifiList.setSelection(0);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}
	    	
	    });

	    Button btn_save = (Button)findViewById(R.id.btn_save_wifi);
	    btn_save.setOnClickListener(btn_save_listener);
    }
    
    private ArrayAdapter getWifiAA(){
    	ArrayAdapter aa = new ArrayAdapter(this, R.layout.wifi_item);
		aa.add("");
		
    	MyWifiAdmin wifi = new MyWifiAdmin(this);
    	List<WifiConfiguration> confList = wifi.getConfiguredNetworks();
		for(WifiConfiguration conf:confList){
			String ssid = MyWifiAdmin.trimSSID(conf.SSID);
			if(ssid!=null && ssid.length()>0){
				aa.add(ssid);	
			}
    	}
		return aa;
    }
    
    private void selectWifi(String SSID){
    	Spinner spinWifiList = (Spinner)findViewById(R.id.spin_wifiList);
    	SpinnerAdapter aa = spinWifiList.getAdapter();
    	for(int i=0;i<aa.getCount();i++){
    		Object o = aa.getItem(i);
    		if(o.equals(SSID)){
    			spinWifiList.setSelection(i);
    			break;
    		}
    	}
    }
    
    private void readWifiSetup(){
		EditText inputSsid = (EditText)findViewById(R.id.text_ssid);
		EditText inputOpenTime = (EditText)findViewById(R.id.text_startTime);
		EditText inputCloseTime = (EditText)findViewById(R.id.text_endTime);
		CheckBox cbForceClose = (CheckBox)findViewById(R.id.cb_forceClose);
	
		SharedPreferences pref = this.getSharedPreferences();
		String startTime = pref.getString("startTime","");
		String endTime = pref.getString("endTime","");
		String SSID = pref.getString("SSID","");
		boolean forceClose = pref.getBoolean("forceClose", false);
		
		if(SSID==null){
			SSID = this.getCurrentSSID();
		}
		
		inputSsid.setText(SSID);
		inputOpenTime.setText(startTime);
		inputCloseTime.setText(endTime);
		cbForceClose.setChecked(forceClose);
	}

	private void saveWifiSetup(){
		EditText inputSsid = (EditText)findViewById(R.id.text_ssid);
		EditText inputStartTime = (EditText)findViewById(R.id.text_startTime);
		EditText inputEndTime = (EditText)findViewById(R.id.text_endTime);
		CheckBox cbForceClose = (CheckBox)findViewById(R.id.cb_forceClose);
		
		String startTime = inputStartTime.getText().toString();
		String endTime = inputEndTime.getText().toString();
		String SSID = inputSsid.getText().toString();
		boolean forceClose = cbForceClose.isChecked();
		
		SharedPreferences pref = this.getSharedPreferences();
		Editor editor = pref.edit();
		editor.putString("SSID", SSID);
		editor.putString("startTime", startTime);
		editor.putString("endTime", endTime);
		editor.putBoolean("forceClose", forceClose);
		editor.commit();
		
		new AlertDialog.Builder(this)
			.setMessage(R.string.save_success)
			.setPositiveButton(R.string.button_ok, null)
			.show();
    }
    
    private String getCurrentSSID(){
    	return null;
    }
    
    private boolean checkAndAdjust(int id){
    	EditText inputTime = (EditText)findViewById(id);
    	String regEx = "([0-2])?([0-9]):([0-5])?([0-9])";
		java.util.regex.Pattern p = java.util.regex.Pattern.compile(regEx);
		java.util.regex.Matcher m = p.matcher(inputTime.getText().toString());
		boolean result = true;
		if(!m.matches()){
			result = false;
			inputTime.setText("");
			inputTime.requestFocus();
		}else{
//			result = m.group(1)!=null && m.group(3)!=null;
			String str = (m.group(1)!=null ? m.group(1) : "0") + m.group(2)+":"+ (m.group(3)!=null ? m.group(3) : "0") +m.group(4);
			inputTime.setText(str);
			if(!result){
				inputTime.requestFocus();	
			}
		}
    	return result;
    }
    
	OnClickListener btn_save_listener=new OnClickListener(){
		public void onClick(View v) {
			boolean ok = checkAndAdjust(R.id.text_endTime) & checkAndAdjust(R.id.text_startTime); 
			if(ok){
				saveWifiSetup();
			}
		}
    };
}
